package services.mes

import models.mes.MesOrdersRepository
import play.api.Logging
import play.api.libs.json.Json

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class MoreMesService @Inject()(
                                mesPost: MoreMesPost,
                                mesRepo: MesOrdersRepository,
                                mesStock: MoreMesStock,
                                mesMo: MoreMesMo,
                                mesBaseDoc: MoreMesBaseDoc
                              )(implicit exec: ExecutionContext)
  extends Logging {

  def deleteOrder(accid: String, formid: String, order: String): Future[String] = {
    def setSend() = {
      logger.debug(s"$formid,setSend($order)")
      formid match {
        case "ppbom" => mesRepo.prdPPBomSetSendSign(order, 0, "")
        case "mo" => mesRepo.prdMoSetSendSign(order, 0, "")
        case _ => mesRepo.stkSetSendFalse(order)
      }
    }

    def result(res: String, msgfail: String) = {
      logger.debug(res)
      try {
        val boolean = {
          val i = res.indexOf("result")
          if (i >= 0) res.substring(i, i + 14).indexOf("true") > 0
          else false
        }
        if (boolean) {
          setSend()
          "Ok"
        }
        else {
          //  mo/ppbom "\"FALSE:单据删除失败或者单据不存在\""
          // stk "\"FALSE:不存在可被删除的开立单据\""
          if (res.indexOf(msgfail) >= 0) {
            setSend()
            "Ok"
          }
          else res
        }
      } catch {
        case e: Exception => logger.error("deleteOrder()", e); res
      }
    }

    formid match {
      case "ppbom" => {
        mesRepo.prdFindMoByPPBom(order).flatMap {
          no => {
            if (no.isEmpty) {
              //              Future(s"用料清单编码($order)未找到，请联系管理员！")
              Future("Ok")
            }
            else mesPost.deleteDoc(doc_no = no.get, doc_type = formid).map {
              r => result(r, "\"FALSE:单据删除失败或者单据不存在\"")
            }
          }
        }
      }
      case "mo" => mesPost.deleteDoc(doc_no = order, doc_type = formid).map {
        r => result(r, "\"FALSE:单据删除失败或者单据不存在\"")
      }
      case _ => mesPost.deleteDoc(doc_no = order, doc_type = formid).map {
        r => result(r, "\"FALSE:不存在可被删除的开立单据\"")
      }
    }

  }

  def syncOrder(accid: String, otype: String, order: String) = {
    logger.debug(s"syncOrder($accid, $otype, $order)")
    otype match {
      case "mo" => mesMo.getMOView(order)
      case "ppbom" => mesMo.ppbom_order(order).map(_.toString)
      case "CGRK" | "XSTH" | "FBDR" | "STRK" | "CGTL" | "XSCK" | "FBDC" | "STTL" => mesStock.putStockInOut2Mes(otype, order)
      case "supplier" => mesBaseDoc.getSupplierList(order)
      case "item" => mesBaseDoc.getMaterialView(order)
      case "bom" => mesBaseDoc.getBomView(order)
      case _ => Future("otype error [mo,ppbom,supplier,item,bom,...]")
    }
  }

  def getOtype() = {
    Future(Json.toJson(MoreMesService.getOtype()).toString())
  }


  def mo_order() = {

  }


}

object MoreMesService {
  def getOtype() = {
    Seq(
      ("mo", "生产订单"),
      ("ppbom", "生产用料清单"),
      ("CGRK", "采购入库单"),
      ("XSTH", "销售退货单"),
      ("FBDR", "分步式调入单"),
      ("STRK", "受托加工材料入库"),
      ("CGTL", "采购退料单"),
      ("XSCK", "销售出库单"),
      ("FBDC", "分步式调出单"),
      ("STTL", "受托加工材料退料"),
      ("supplier", "供应商"),
      ("item", "物料"),
      ("bom", "物料清单")
    )
  }
}


